package cn.miaopasi.boot.clickhouse.config.exception;

import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理器
 *
 * @author lixin
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*** 参数异常拦截 */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public SaResult missingParameterHandler(MissingServletRequestParameterException e) {
        String paramsName = e.getParameterName();
        return SaResult.error(StrUtil.format("参数[{}]不存在", paramsName));
    }

    /*** 404异常拦截 */
    @ExceptionHandler(NoHandlerFoundException.class)
    public SaResult noFoundHandler(NoHandlerFoundException e) {
        String errorMsg = StrUtil.format("请求路径[{}]不存在", e.getRequestURL());
        return SaResult.error(errorMsg);
    }

    /*** 登录异常拦截 */
    @ExceptionHandler(SaTokenException.class)
    public SaResult saTokenException(SaTokenException e) {
        e.getStackTrace();
        return SaResult.error(e.getMessage());
    }

}
